package com.glela.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.exception.PlatformException;
import com.glela.order.model.response.OrderObjectVo;
import com.glela.order.service.OrderObjectService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestPageBaseModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;

/**
 * @author chengzhequan
 * @2016年10月28日17:53:17
 */
@Controller
@RequestMapping("/orderObject")
public class OrderObjectController {

	private static Logger logger = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	private OrderObjectService orderObjectService;
	
	/**
	 * 查询我的订单列表
	 * 
	 * @author 程哲权
	 * 
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/listOrderObjectDaiZhiFuByUserId", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> listOrderObjectDaiZhiFuByUserId(@RequestBody RequestPageBaseModel requestPageBaseModel) {
		try {
			if (ParameterUtils.validParams(requestPageBaseModel.getUserId(),
					requestPageBaseModel.getPageNum(),requestPageBaseModel.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			List<OrderObjectVo> orderObjectVoList = orderObjectService.listOrderObjectDaiZhiFuByUserId(requestPageBaseModel.getUserId(),
					requestPageBaseModel.getStartIndex(),requestPageBaseModel.getPageSize());
			return new ResponseEntity<>(new ResponseResultModel<List<OrderObjectVo>>(orderObjectVoList), HttpStatus.OK);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 根据订单号查询待支付订单信息
	 * 
	 * @author 程哲权
	 * 
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/getOrderObjectEntityByOrderSn", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getOrderObjectEntityByOrderSn(HttpServletRequest request, @RequestBody RequestPageBaseModel requestPageBaseModel) {
		try {
			
			if (ParameterUtils.validParams(request.getHeader("phoneType"),requestPageBaseModel.getOrderSn())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			OrderObjectVo orderObjectVo = orderObjectService.getOrderObjectEntityByOrderSn(request.getHeader("phoneType"),requestPageBaseModel.getOrderSn(),requestPageBaseModel.getUserId());
			if (orderObjectVo==null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20141), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<OrderObjectVo>(orderObjectVo), HttpStatus.OK);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 根据订单号查询待支付订单信息（新）
	 *
	 * @author zhangrnejie  2017-09-01
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getOrderObjectEntityByOrderSnNew", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getOrderObjectEntityByOrderSnNew(HttpServletRequest request, @RequestBody RequestPageBaseModel reqModel) {
		try {
			reqModel.initHeader(request);
			if (ParameterUtils.validParams(reqModel.getOrderSn(), reqModel.getReqHeader().getPhoneType(), reqModel.getReqHeader().getAppSystemVersion())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}

			OrderObjectVo orderObjectVo = orderObjectService.getOrderObjectEntityByOrderSnNew(reqModel);
			if (orderObjectVo==null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20141), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<OrderObjectVo>(orderObjectVo), HttpStatus.OK);
		} catch (PlatformException e) {
			logger.warn("待支付订单详情异常：{}", e.getMessage());
			return new ResponseEntity<>(new ResponseBaseModel(e.getErrorEnum(), e.getMessage()), HttpStatus.OK);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
}
